/**
 * Created on 11 avr. 08 by Sebastian Audet
 */
package tile;

import java.io.File;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.thereconstruction.tileset.Tiles;

/**
 * @author Sebastian Audet
 */
public abstract class AbstractTileSet implements TileSet {

    protected ResourceBundle bundle;
    protected JAXBContext context;
    protected Unmarshaller unmarshaller;
    protected Tiles tiles;
    protected File xmlfile;
    /*
     * My current rationale in making the above variables protected is to enable
     * implementors to do what they like with the xml, in particular XMLTileSet 
     * is given full access to the xml bindings, allowing for more dynamic 
     * tilesets, using only xml and no unnecessary java. 
     */

    AbstractTileSet() {
        try {
            try {
                bundle = PropertyResourceBundle.getBundle(this.getClass().getName() + "ResourceBundle");
            } catch (MissingResourceException e) {
                e.printStackTrace();
            }
            /*
             * I hope to eventually deprecate this, however totally moving to xml
             * would require some deeper code changes than I'm able to commit to at
             * the moment, so for now I'm just keeping the old Resource Bundles
             */
            context = JAXBContext.newInstance("org.thereconstruction.tileset");
            unmarshaller = context.createUnmarshaller();
            xmlfile = new File(this.getClass().getSimpleName() + ".xml");
            if (xmlfile.exists()) {
                tiles = (Tiles) unmarshaller.unmarshal(xmlfile);
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see tile.TileSet#getLevel()
     */
    public abstract int getLevel();

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(TileSet o) {
        int temp1 = getLevel();
        int temp2 = o.getLevel();
        if (temp1 > temp2) {
            return 1;
        } else if (temp1 == temp2) {
            return 0;
        } else {
            return -1;
        }
    }

    public File getResource(String resourceidentifier) {
        if (bundle != null && bundle.containsKey(resourceidentifier)) {
            return new File("tile" + File.separator + bundle.getString(resourceidentifier));
        } else if (xmlfile.exists()) {
            for (Tiles.Tile tile : tiles.getTile()) {
                if (tile.getSymbol().equals(resourceidentifier)) {
                    //System.out.println(new File("tile" + File.separator + tile.getFile()));
                    return new File("tile" + File.separator + tile.getFile());
                }
            }
        }

        return null;
    }
}
